package com.xinli;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class TestBall extends Activity {
	SensorManager sensorManager = null;
	Sensor sensor = null;
	private boolean init = false;
	private int container_width = 0;   	
	private int container_height = 0;   
	private int ball_width = 0;   
	private int ball_height = 0;   
	private BallView ball;
	private TextView tv;
	
	private float ballX = 100;
	private float ballY = 100;
	
	private static final float MAX_ACCELEROMETER = 9.81f;   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.main);
//       tv = (TextView) findViewById(R.id.textview);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        
    }
    
    
    // x, y is between [-MAX_ACCELEROMETER, MAX_ACCELEROMETER]   
    void moveTo(float x, float y) {   
    	ballX +=x;
    	ballY +=y;
    	
    	if (ballX < 0 ){
    		ballX = 0;
    	}
    	
    	if (ballY < 0){
    		ballY = 0;
    	}
    	
    	if (ballX > container_width - ball_width){
    		ballX = container_width - ball_width;
    	}
    	
    	if (ballY > container_height - ball_height){
    		ballY = container_height - ball_height;
    	}
    	
    	ball.moveTo((int)ballX, (int)ballY);
    	Log.v("ball", "ball x="+ballX+" ball y="+ballY);
    }   
    
//    void translate(int pixelX, int pixelY) {   
//        int x = pixelX + container_width / 2 - ball_width / 2;   
//        int y = pixelY + container_height / 2 - ball_height / 2;   
//        ball.moveTo(x, y);   
//    }   
    
    public void register(){
    	sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
    }
    
    public void unregister(){
    	sensorManager.unregisterListener(listener);
    }
    
    SensorEventListener listener = new SensorEventListener(){

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			if (!init)
				return;
			float x = event.values[SensorManager.DATA_X]*3;
			float y = event.values[SensorManager.DATA_Y]*3;
			float z = event.values[SensorManager.DATA_Z];
//			tv.setText("sensor X="+x+" Y="+y+" Z="+z);
			moveTo(-x,y);
		}
    	
    };

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		if(hasFocus && !init){
			init();
			init = true;
		}
	}
	
	
	
	
	public void init(){
			View container = findViewById(R.id.ball_container);   
	        container_width = container.getWidth();   
	        container_height = container.getHeight();   
	        Log.v("test", "conatiner x="+container_width+" container y="+container_height);
	        ball = (BallView) findViewById(R.id.ball);   
	        ball_width = ball.getWidth();   
	        ball_height = ball.getHeight();   
	        Log.v("test", "ball x ="+ball_width+" y="+ball_height);
	        moveTo(0, 0);   
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregister();
	}


	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregister();
	}


	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		register();
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		register();
	}
    
	
	
    
}